package com.zzy.util;



import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.ActivitiEngineAgenda;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityManager;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;

import java.io.InputStream;

public class MoveNodeCmd implements Command<Object> {

	private String currentTaskId;
    private String targetNode;
    private String deleteReason;//退回的时候，删除任务，的说明
    private boolean isDelHis;

    public MoveNodeCmd(){

    }
    public MoveNodeCmd(String currentTaskId,String targetNode,String deleteReason, boolean isDelHis){
        this.currentTaskId = currentTaskId;
        this.targetNode = targetNode;
        this.deleteReason = deleteReason;
        this.isDelHis = isDelHis;
    }


    public String getCurrentTaskId() {
        return currentTaskId;
    }

    public void setCurrentTaskId(String currentTaskId) {
        this.currentTaskId = currentTaskId;
    }

    public String getTargetNode() {
        return targetNode;
    }

    public void setTargetNode(String targetNode) {
        this.targetNode = targetNode;
    }

    public String getDeleteReason() {
        return deleteReason;
    }

    public void setDeleteReason(String deleteReason) {
        this.deleteReason = deleteReason;
    }

    public boolean isDelHis() {
        return isDelHis;
    }

    public void setDelHis(boolean delHis) {
        isDelHis = delHis;
    }

    @Override
    public Object execute(CommandContext commandContext) {
        ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
        TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
        TaskEntity taskEntity = taskEntityManager.findById(this.currentTaskId);
        ExecutionEntity executionEntity = executionEntityManager.findById(taskEntity.getExecutionId());
        Process process = ProcessDefinitionUtil.getProcess(executionEntity.getProcessDefinitionId());
        taskEntityManager.deleteTask(taskEntity,this.deleteReason,this.isDelHis,false);//不删除当前任务历史数据
        FlowElement targetFlowElement = process.getFlowElement(targetNode);
        executionEntity.setCurrentFlowElement(targetFlowElement);
        ActivitiEngineAgenda activitiEngineAgenda = commandContext.getAgenda();
        activitiEngineAgenda.planContinueProcessInCompensation(executionEntity);
        return null;
    }
}
